<?php
namespace app\admin\model;

use think\Model;
use app\admin\model\Channel;

class BusRechargegApply extends Model
{
    public function getListAll($request)
    {
        $data = $this->alias('ra')
        ->field('ra.*,concat( bus_channel.channel_id , "/", bus_channel.company_name ) as company_name,adm_user.name,adm_user.username')
        ->join('adm_user','ra.apply_user_id = adm_user.id')
        ->join('bus_channel','ra.recharge_channel_id = bus_channel.channel_id');

        switch (session('user_type')) {
            case 'admin':
                # code...
                break;
            case 'clerk':
                $data = $data->where('ra.apply_user_id',session('user_id'));
                break;

            default:
                exit;
                break;
        }

        if($request['status'] == 0)
        {
            $data = $data->where("ra.status",0);
        }elseif($request['status'] == 1){
            $data = $data->where("ra.status",'in',[1,2]);
        }

        $data = $data->order('ra.id desc')
        ->paginate($request['limit'])
        ->toarray();

        foreach ($data['data'] as $key => $value) {
            switch ($value['status']) {
                case '0':
                    $data['data'][$key]['status'] = '待审核';
                    break;
                case '1':
                    $data['data'][$key]['status'] = '通过';
                    break;
                case '2':
                    $data['data'][$key]['status'] = '拒绝';
                    break;

                default:
                    # code...
                    break;
            }
            switch ($value['type']) {
                case '1':
                    $data['data'][$key]['type'] = '加款';
                    break;
                case '2':
                    $data['data'][$key]['type'] = '减款';
                    break;
            }

            $data['data'][$key]['name'] = $value['name'] . '(' . $value['username'];
        }

        return $data;

    }

    public function examineAndApprove($request)
    {
        $Channel = new Channel;

        $oneApprove = $this->where('id',$request['id'])->find();

        if($oneApprove){
            $resule = true;

            if($request['status'] == 1)
            {
                switch ($oneApprove['type']) {
                    case 1:
                        //加款
                        $resule = $Channel->setSmsBalance($oneApprove['recharge_channel_id'],$oneApprove['amount'],'审核加款['.$request['statusdes'].']');
                        break;
                    case 2:
                        //减款
                        $resule = $Channel->setBalanceRefund($oneApprove['recharge_channel_id'],$oneApprove['amount'],'审核减款款['.$request['statusdes'].']');
                        break;

                    default:
                        # code...
                        break;
                }
            }

            if($resule){
                $this->where('id',$request['id'])->update(['status'=>$request['status'],'statusdes'=>$request['statusdes'],'apply_time'=>date('Y-m-d H:i:s'),'audit_user'=>session('username')]);
                return true;
            }
        }

        return false;

    }
}